// 主要JavaScript功能
document.addEventListener('DOMContentLoaded', function() {
    // 初始化Bootstrap tooltips
    const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
    tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl)
    });

    // 动态评分系统
    const ratingForm = document.querySelector('.rating-form');
    if (ratingForm) {
        const ratingInputs = ratingForm.querySelectorAll('input[name="rating"]');
        const submitButton = ratingForm.querySelector('button[type="submit"]');
        
        ratingInputs.forEach(input => {
            input.addEventListener('change', function() {
                submitButton.disabled = false;
            });
        });
    }

    // 添加导航活动状态
    const currentLocation = window.location.pathname;
    const navLinks = document.querySelectorAll('.navbar-nav .nav-link');
    
    navLinks.forEach(link => {
        if (link.getAttribute('href') === currentLocation) {
            link.classList.add('active');
        }
    });
});

// 电影搜索建议
function initializeSearchAutocomplete() {
    const searchInput = document.getElementById('id_query');
    if (!searchInput) return;

    searchInput.addEventListener('input', debounce(function() {
        const query = searchInput.value.trim();
        if (query.length < 2) return;
        
        fetch(`/movies/search-suggest/?q=${encodeURIComponent(query)}`)
            .then(response => response.json())
            .then(data => {
                // 这里可以实现自动完成建议
                console.log(data);
            })
            .catch(error => console.error('搜索建议错误:', error));
    }, 300));
}

// 防抖函数
function debounce(func, wait) {
    let timeout;
    return function() {
        const context = this, args = arguments;
        clearTimeout(timeout);
        timeout = setTimeout(() => {
            func.apply(context, args);
        }, wait);
    };
}

// 初始化搜索自动完成
initializeSearchAutocomplete(); 